package com.medicalsystem.service;

import com.medicalsystem.entity.Appointment;
import com.medicalsystem.repository.AppointmentRepository;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class AppointmentService {

    private final AppointmentRepository appointmentRepository;

    public AppointmentService(AppointmentRepository appointmentRepository) {
        this.appointmentRepository = appointmentRepository;
    }

    // 获取所有预约信息
    public List<Appointment> getAllAppointments() {
        return appointmentRepository.findAll();
    }

    // 根据ID获取预约信息
    public Appointment getAppointmentById(Long id) {
        return appointmentRepository.findById(id).orElse(null);
    }

    // 新增预约信息
    public Appointment saveAppointment(Appointment appointment) {
        return appointmentRepository.save(appointment);
    }

    // 修改预约信息
    public Appointment updateAppointment(Long id, Appointment updatedAppointment) {
        Optional<Appointment> existingAppointment = appointmentRepository.findById(id);
        if (existingAppointment.isPresent()) {
            Appointment appointment = existingAppointment.get();
            appointment.setPatientId(updatedAppointment.getPatientId());
            appointment.setPatientName(updatedAppointment.getPatientName());
            appointment.setDoctorId(updatedAppointment.getDoctorId());
            appointment.setDoctorName(updatedAppointment.getDoctorName());
            appointment.setDepartment(updatedAppointment.getDepartment());
            appointment.setAppointmentDate(updatedAppointment.getAppointmentDate());
            appointment.setAppointmentTime(updatedAppointment.getAppointmentTime());
            appointment.setStatus(updatedAppointment.getStatus());
            appointment.setFee(updatedAppointment.getFee());
            appointment.setSymptoms(updatedAppointment.getSymptoms());
            appointment.setRemark(updatedAppointment.getRemark());
            appointment.setUpdateTime(new Date());
            return appointmentRepository.save(appointment);
        }
        return null;
    }

    // 根据ID删除预约信息
    public void deleteAppointment(Long id) {
        appointmentRepository.deleteById(id);
    }
}
